﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.IO;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web.Mvc;
using System.Globalization;
using System.Net;
using System.Net.NetworkInformation;
using ParticipacionVecinalWeb.Models.ParticipacionVecinal;
using BusinessEntities;
using System.Web.Script.Serialization;

namespace ParticipacionVecinalWeb.Models {
    public class Utils {

        //Obtiene Titulo de la Pagina
        public string getTitle() {
            return "Municipalidad San Isidro";
        }

        //Obtiene Ruta Pagina
        public string getPath() {
            return HttpContext.Current.Request.Url.Scheme + System.Uri.SchemeDelimiter + HttpContext.Current.Request.Url.Authority + HttpContext.Current.Request.ApplicationPath + ((HttpContext.Current.Request.ApplicationPath.EndsWith("/")) ? "" : "/");
        }

        //Obtenemos la ruta fisica
        public string getPhysicalPath() {
            return HttpContext.Current.Request.PhysicalApplicationPath;
        }

        //Obtenemos la ruta de la imagen
        public string getImage(string image) {
            return this.getPath() + "Content/img/" + image;
        }

        //Obtenemos la ruta de la imagen
        public string getImageUsuario(string image) {
            return this.getPath() + "Upload/Usuario/" + image;
        }

        //Insertamos los archivos dinamicamente
        private ArrayList readFile(string sourceDir, bool fRecursive) {

            ArrayList script = new ArrayList();

            int i;
            int posSep;
            string sDir;
            string[] aDirs;

            if (!sourceDir.EndsWith("/")) {
                sourceDir += "/";

            }


            if (fRecursive) {
                aDirs = System.IO.Directory.GetDirectories(this.getPhysicalPath() + sourceDir);
                for (i = 0; i <= aDirs.GetUpperBound(0); i++) {
                    posSep = aDirs[i].LastIndexOf("/");
                    sDir = aDirs[i].Substring((posSep + 1), aDirs[i].Length - (posSep + 1));
                    script.AddRange(readFile(sourceDir + sDir, fRecursive));
                }
            }

            string[] tFiles;
            tFiles = System.IO.Directory.GetFiles(this.getPhysicalPath() + sourceDir);
            Int32 j = 0;
            Int32 n = 0;
            n = tFiles.Length - 1;
            try {

                for (j = 0; j <= n; j++) {
                    System.IO.FileInfo afile = new System.IO.FileInfo(tFiles[j]);
                    script.Add(getPath() + sourceDir + afile.Name);
                }

            } catch {

            }
            script.Sort();
            return script;
        }

        //Registramos los Scripts
        public string getScript(string PathJs) {

            ArrayList files = readFile(PathJs, true);
            string script = System.Environment.NewLine;
            string Tab = "\t";

            foreach (string element in files) {

                script = script + "" + Tab + "<script type='text/javascript' src='" + element.ToString() + "'></script>" + System.Environment.NewLine;
            }

            return script;

        }

        public string getVersionSistema() {
            string version = System.Configuration.ConfigurationManager.AppSettings["VersionSistema"];

            return version;

        }

        /// <summary>
        /// Gets the services get.
        /// </summary>
        /// <param name="action">The action.</param>
        /// <param name="prm">The PRM.</param>
        /// <returns></returns>
        public String getServicesGet(String action, String prm) {
            try {
                String url = ConfigurationManager.AppSettings["UrlServicio"].ToString() + action + prm;
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                req.Method = "GET";
                HttpWebResponse res = (HttpWebResponse)req.GetResponse();
                StreamReader reader = new StreamReader(res.GetResponseStream());
                String strJson = reader.ReadToEnd();
                return strJson;
            } catch {
                return "";
            }
        }

        public List<IV> getVecinos(Parametros oParametros) {
            //http://localhost:5623/api/Vecino?texto=&doc=&tipo=0&pagina=1&paginacion=10
            //http://localhost/sismuni/api/Vecino?texto=&doc=&tipo=0&pagina=1&paginacion=10
       
            String strParametro = "?texto=" + oParametros.Texto + "&doc=" + oParametros.Codigo;
            strParametro += "&tipo=" + oParametros.IdCombo + "&pagina=" + oParametros.Pagina + "&paginacion=" + oParametros.Paginacion;
            String data = this.getServicesGet("Vecino", strParametro);

            JavaScriptSerializer js = new JavaScriptSerializer();
            var lstvecino = js.Deserialize<List<ParticipacionVecinal.Vecino>>(data);

            var lst = (from x in lstvecino
                       select new IV {
                           ID = x.Id,
                           C = x.NroDoc,
                           R = x.Nombre + " " + x.NombreSecundario + " " + x.Paterno + " " + x.Materno,
                           DE = x.Direccion,
                           A = x.Total
                       }).ToList();
            return lst;
        }


    }

}